package com.smg.crm.settings.web.controller;

import com.smg.crm.settings.domain.DicType;
import com.smg.crm.settings.service.DictionaryTypeService;
import com.smg.crm.utils.HandleFlag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/settings/dictionary")
public class DictionaryController {

    @Autowired
    private DictionaryTypeService dictionaryTypeService;

    /**
     * 跳转到字典模块的首页面
     * @return
     */
    @RequestMapping("/toIndex.do")
    public String toIndex(){
        return "/settings/dictionary/index";
    }

    /**
     * 跳转到字典类型模块的首页面
     * @return
     */
    @RequestMapping("/type/toIndex.do")
    public String toTypeIndex(Model model){

        //查询后台的数据,字典类型数据列表
        List<DicType> dicTypeList = dictionaryTypeService.findAll();

        System.out.println("dicTypeList::"+dicTypeList);

        //将列表数据封装到model中
        model.addAttribute("dicTypeList",dicTypeList);

        return "/settings/dictionary/type/index";
    }


    @RequestMapping("/type/toEdit.do")
    public ModelAndView toEdit(){

        ModelAndView mv = new ModelAndView();

        mv.setViewName("/settings/dictionary/type/save");

        return mv;
    }

    /**
     * 根据code查询数据库中是否有存在的数据
     *      返回json数据
     * @param code
     * @return
     */
    @RequestMapping("/type/checkCode.do")
    @ResponseBody
    public Map<String,Object> checkCode(String code){
        boolean flag = dictionaryTypeService.checkCode(code);

        if(flag){
            //可以新增
            //{success:true}
            return HandleFlag.successTrue();
        }else{
            //已重复,无法新增
            //{success:false,msg:"编码已重复"}
            return HandleFlag.error("编码已重复");

        }
    }



    //-------------------上面是字典类型控制器-----------------

    // ------------------下面是字典值控制器-------------------




    /**
     * 跳转到字典值模块的首页面
     * @return
     */
    @RequestMapping("/value/toIndex.do")
    public String toValueIndex(){
        return "/settings/dictionary/value/index";
    }



}
